﻿using NightskyMimic.Core;
using NightskyMimic.Domain;
using NightskyMimic.Resources;
using NightskyMimic.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Main
{
    //generates a csv with certain number of games
    //aiDeck;opponentDeck;numberOfTurns;win
    //if ai win, 1 else, 0
    public class Experiments
    {
        private List<Zone> _zones;
        private int _numberOfGames;
        int wins;

        public Experiments()
        {
            clearZones();
            _numberOfGames = 100;
            wins = 0;
        }

        public String randomExperiment()
        {
            String result = String.Empty;
            Strategy ai = new AlphaBeta();
            Strategy random = new Aggressive();
            

            for (int i = 0; i < _numberOfGames; i++)
            {
                int zone1 = Unicorn.Dash(0, 4);
                int zone2 = Unicorn.Dash(0, 4);

                Player player = new Player("ai", _zones[zone1], ai);
                Player opponent = new Player("opponent", _zones[zone2], random);

                Controller c;
                if (Unicorn.Dash(0, 10) < 5)
                    c = new Controller(player, opponent);
                else
                    c = new Controller(opponent, player);

                Player winner = c.Start();

               result+= generateMetrics(winner, zone1, zone2);
               clearZones();
               Console.WriteLine(" - - - - - - - - - " + i + " - - - - - - - - - ");
            }
            return result;
        }

        //aiDeck;opponentDeck;win
        //if ai win, 1 else, 0
        private string generateMetrics(Player winner, int zone1, int zone2)
        {
            String result = String.Empty;

            int aiWin = winner.Name == "ai" ? 1 : 0;
            wins += aiWin;
            Console.WriteLine(aiWin);
            Console.WriteLine(" "); 
            result += chooseDeck(zone1) + ";" + chooseDeck(zone2) + ";" + aiWin + "\n";

            return result;
        }

        private object chooseDeck(int zone)
        {
            if (zone == 0)
                return "black";
            else if (zone == 1)
                return "green";
            else if (zone == 2)
                return "red";
            else return "white";
        }

        private void clearZones()
        {
            _zones = new List<Zone>();
            _zones.Add(new Zone(BlackVanilla.BlackVanillaDeck()));
            _zones.Add(new Zone(GreenVanilla.GreenVanillaDeck()));
            _zones.Add(new Zone(RedVanilla.RedVanillaDeck()));
            _zones.Add(new Zone(WhiteVanilla.WhiteVanillaDeck()));
        }
    }
}
